using System;
using System.Collections.Generic;
using System.IO;

namespace Seagull.Generic
{
    public static class Extensions
    {
        public static Optional<TValue> TryGetValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
        {
            TValue value;
            return dictionary.TryGetValue(key, out value) ? Optional.Some(value) : Optional.None;
        }

        public static DateTime TruncateToSecond(this DateTime dateTime)
        {
            return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day,
                dateTime.Hour, dateTime.Minute, dateTime.Second);
        }

        public static DateTime TruncateToHour(this DateTime dateTime)
        {
            return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day,
                dateTime.Hour, 0, 0);
        }

        public static DateTime RoundToHour(this DateTime dateTime)
        {
            var dateTime1 = dateTime.AddMinutes(30);
            return new DateTime(dateTime1.Year, dateTime1.Month, dateTime1.Day, dateTime1.Hour, 0, 0);
        }

        public static double UsedPercentage(this DriveInfo driveInfo)
        {
            return (1 - driveInfo.AvailableFreeSpace * 1.0 / driveInfo.TotalSize);
        }
    }
}